$(document).ready(function () {
    /*查询科目*/
    $.ajax({
        type: "POST",
        url: "fields",
        data: {},
        dataType: "json",
        success: function (data) {
            for (var i = 0; i < data.length; i++) {
                $("#kindId").html($("#kindId").html() + '<option value="' + data[i].fieldId + '")>' + data[i].fieldName + '</option>');
                $("#kindId2").html($("#kindId2").html() + '<option value="' + data[i].fieldId + '")>' + data[i].fieldName + '</option>');
            }
        }
    });

    /*查询等级*/
    $.ajax({
        type: "POST",
        url: "ranks",
        data: {},
        dataType: "json",
        success: function (data) {
            for (var i = 0; i < data.length; i++) {
                $("#rankId").html($("#rankId").html() + '<option value="' + data[i].rankId + '")>' + data[i].rankName + '</option>');
            }
        }
    });
    /*查询考试时间*/
    $.ajax({
        type: "POST",
        url: "startTime",
        data: {},
        dataType: "json",
        success: function (data) {
            for (var i = 0; i < data.length; i++) {
                $("#starttime").html($("#starttime").html() + '<option value="' + data[i].gradeId + '")>' + data[i].startTime + '</option>');
                $("#starttime2").html($("#starttime2").html() + '<option value="' + data[i].gradeId + '")>' + data[i].startTime + '</option>');
            }
        }
    });
    search();
    search2();
});

function search() {
    var studentname = $("#studentname").val();
    var kindId1 = $("#kindId").val();
    var rankId = $("#rankId").val();
    var starttime = $("#starttime").find("option:selected").text();
 //   var starttime = $("#starttime").val();
    $.ajax({
        type: "POST",
        url: "List",
        data: {"studentname": studentname, "kindId": kindId1,"rankId":rankId,"starttime":starttime},
        //   data: {"studentname": "",  "kindId": 1},
        dataType: "json",
        success: function (data) {
            var str = "";
            if (data.length > 0) {
                jQuery.each(data, function (i, item) {
                    var index = i + 1;
                    str += "<tr class=\"warning\">";
                    str += "<td>" + index + "</td>";
                    str += "<td>" + item.user.userName + "</td>";
                    str += "<td>" + item.field.fieldName + "</td>";
                    str += "<td>" + item.grade + "</td>";
                    str += "<td>" + item.rank.rankName + "</td>";
                    str += "<td>" + item.createTime + "</td>";
                    str += "</tr>";
                    $("#tablist").html(str);
                });
            }
            else {
                $("#tablist").html("");
            }
            //执行分页函数,
            $(function () {
                //设置页数按钮所在的层,
                $("div.holder").jPages({
                    //显示数据所在的块的ID
                    containerID: "tablist",
                    first: '首页',
                    last: '尾页',
                    previous: '上页',
                    next: '下页',
                    //每页显示数据为多少行
                    perPage: 4,
                    //起始页
                    startPage: 1,
                    //开始页码为2个
                    startRange: 1,
                    //最多显示几个页码页码,其余用..代替
                    midRange: 4,
                    //结束页码为1个
                    endRange: 1,
                    //数据显示的动画效果
                    animation: 'wobble',
                    keyBrowse: true
                });
            });
        },
        error: function (XMLHttpRequest, textStatus) {
            //   alert(XMLHttpRequest.status);
            //    alert(XMLHttpRequest.readyState);
            //    alert(textStatus);
            layer.alert("数据读取错误!");
        }
    });
}

//统计图条件搜索
function search2(){
    var kindId2 = $("#kindId2").val();
    var starttime2 = $("#starttime2").find("option:selected").text();
    $.ajax({
        type: "POST",
        url: "statictic",
        data: {"kindId2":kindId2, "starttime2":starttime2},
    //    data: {"kindId2":1, "starttime2":"2017-03-19 15:51:10"},
        dataType: "json",
        success:function(data){
        //    alert(data);
            var z = new Array();
            var x = new Array();
            z = data[0].split(",");
            x = data[1].split(",");
            var myhar = echarts.init(document.getElementById("gradeStatistic"));
            var option = {
                title: {
                    text: '科目成绩统计',
                    subtext: '等级判定：0-59 不及格， 60-75 及格， 76-85 良好， 86-100 优秀',
                    x: 'center'
                },
                color: ['#3398DB'],
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                        type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                    }
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                xAxis: [
                    {
                        type: 'category',
                        data: z,  //横坐标值
                        axisTick: {
                            alignWithLabel: true
                        }
                    }
                ],

                /*纵坐标最小为0，最大为400，分为10段*/
                yAxis: [
                    {
                        //   type: 'value'
                        scale: true,
                        power: 1,
                        precision: 1,
                        splitNumber: 9,
                        min: 0,
                        max: 110
                    }
                ],
                series: [
                    {
                        name: '学生分数',
                        type: 'bar',
                        barWidth: '30%',
                        data: x //滑动显示数量
                    }
                ]
            };
            myhar.setOption(option);
        },
        error:function(data){
            layer.alert("数据读取错误2");
        }
    });
}
